package com.wmyg.antifake.service.impl;

import com.wmyg.antifake.bean.Picture;
import com.wmyg.antifake.bean.PictureConvert;
import com.wmyg.antifake.dao.PictureMapper;
import com.wmyg.antifake.service.PictureService;
import com.wmyg.antifake.utils.PicConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("pictureService")
public class PictureServiceImpl implements PictureService {

    @Autowired
    private PictureMapper pictureMapper;


    @Override
    public List<Picture> selectPic(int targetType, String targetId, String picType) {
        Integer picType1 = StringUtils.isEmpty(picType) ? null : Integer.parseInt(picType);
        List<Picture> list = pictureMapper.selectPic(targetType,targetId,picType1);
        if(list.size()==0){
            return null;
        }
        return list;
    }

    @Override
    public PictureConvert selectPicConvert(int targetType, String targetId, String picType) {
        if(StringUtils.startsWith(targetId,"0")){
            targetId = targetId.substring(1);
        }
        List<Picture> list = selectPic(targetType,targetId,picType);
        if(list == null || list.size() == 0){
            return  new PictureConvert();
        }
        PictureConvert pictureConvert=PicConvertUtils.getPictureConvert(list);
        return pictureConvert;
    }

    @Override
    public int deletePrimaryId(int picId) {
        return pictureMapper.deletePrimaryId(picId);
    }

    @Override
    public Picture selectPrimaryId(int picId) {

        return pictureMapper.selectPrimaryId(picId);
    }

}
